    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.thelostlands.plugins.TheLostKindness.CommandHandler;

import net.thelostlands.plugins.TheLostKindness.KindPlayer;
import net.thelostlands.plugins.TheLostKindness.Main;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.logging.Logger;

/**
 *
 * @author fabio
 */
public class CommandHandler implements CommandExecutor {

    static final Logger log = Main.log;
    Main main;

    public CommandHandler(Main main) {
        this.main = main;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String commandlabel, String[] args) {
        Player s = (Player) sender;
        String r;
        if (args.length > 1) {
            r = args[1];
        } else {
            r = s.getName();
        }

        if (command.getName().compareTo("kind") == 0) {
            if (args == null || args.length == 0) {
                return false;
            }
            if (args[0].compareTo("give") == 0) {//give
                give(r, s);
                return true;
            } else if (args[0].compareTo("stat") == 0) {//howmany
                howMany(s);
                return true;
            } else if (args[0].compareTo("list") == 0) {//hitList
                hitList(s);
                return true;
            }
        }
        return false;
    }

    /**
     * 
     * @param receiver The receiver of the point
     * @param sender The sender of the point
     * @return True if receiver has obtained 1 point
     */
    private boolean give(String receiver, Player sender) {
        KindPlayer r = Main.players.get(receiver);
        KindPlayer s = Main.players.get(sender.getName());

        if (r != null && s != null) {
            if (r.hasAPoint()) {
                s.giveAPointTo(r);
            }
            sender.sendMessage("Hai appena assegnato un punto a " + r);
        } else if (r == null) {
            sender.sendMessage("Il nome utente inserito non è corretto o non esiste !");
            log.info(sender.getName() + " ha inserito il nickname non corretto, [TheLostKindness]");
            return false;

        } else if (s == null) {
            sender.sendMessage("Si sono verificati degli errori, riprovare più tardi");
            log.info(sender.getName() + " ha fatto verificare degli errori, [TheLostKindness]");
            return false;
        }
        return true;
    }

    /**
     * 
     * @param sender The sender of the command
     * @param player The target player
     * @return How many points player has and his position.
     */
    private String howMany(Player sender) {
        KindPlayer p = Main.players.get(sender.getName());
        sender.sendMessage("Hai accumulato" + p.getPoints() + "punti");
        log.info(sender.getName() + " ha richiesto la visualizzazione dei suoi punti, [TheLostKindness]");
        return null;
    }

    /**
     * 
     * @param sender The sender of the command
     * @return The first 3 player sorted by points.
     */
    private String hitList(Player sender) {
        String n = System.getProperty("line.separator");
        sender.sendMessage("La TOP 3 degli utenti con più voti:" + KindPlayer.getHits(0) + n + KindPlayer.getHits(1) + n + KindPlayer.getHits(2));
        log.info(sender.getName() + " ha richiesto la visualizzazione della TOP 3, [TheLostKindness]");
        return null;
    }
}